package com.liu.stockmarket

import android.content.Context
import android.content.res.Resources
import android.content.res.Resources.NotFoundException
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.liu.stockmarket.databinding.RvEmptyViewBinding
import com.liu.stockmarket.utils.toast
import okhttp3.internal.notify


/**
 * 所有Activity都应该继承
 */
abstract class BaseActivity : AppCompatActivity() {
    lateinit var emptyViewBinding: RvEmptyViewBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        emptyViewBinding = RvEmptyViewBinding.inflate(layoutInflater)
        initView(savedInstanceState)
        observer()
        listener()

        emptyViewBinding.root.setOnClickListener {
            emptyViewOnListener()
        }

//        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//        window.statusBarColor = getColor(R.color.color_primary)
//        window.navigationBarColor = Color.TRANSPARENT
//        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
//                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

        window.statusBarColor = getColor(R.color.color_primary)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);       //设置沉浸式状态栏，在MIUI系统中，状态栏背景透明。原生系统中，状态栏背景半透明。
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


    }

    fun getNavigationBarHeight():Int{
        return getInternalDimensionSize("navigation_bar_height")
    }

    fun getStatusBarHeight(): Int {
        return getInternalDimensionSize("status_bar_height")
    }

    private  fun getInternalDimensionSize(key: String): Int {
        val result = 0
        try {
            val resourceId = Resources.getSystem().getIdentifier(key, "dimen", "android")
            if (resourceId > 0) {
                val sizeOne = resources.getDimensionPixelSize(resourceId)
                val sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId)
                return if (sizeTwo >= sizeOne) {
                    sizeTwo
                } else {
                    val densityOne = resources.displayMetrics.density
                    val densityTwo = Resources.getSystem().displayMetrics.density
                    val f = sizeOne * densityTwo / densityOne
                    (if (f >= 0) f + 0.5f else f - 0.5f).toInt()
                }
            }
        } catch (ignored: NotFoundException) {
            return 0
        }
        return result
    }

    /**
     * 初始化
     */
    abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 事件
     */
    abstract fun listener()

    /**
     * 订阅
     */
    abstract fun observer()

    fun setToolbar(toolbar: Toolbar, showNavigation: Boolean = true,addToolbarHeight:Boolean=true) {
        setSupportActionBar(toolbar)
        if (showNavigation) {
            toolbar.setNavigationOnClickListener {
                finish()
            }
        } else {
            toolbar.navigationIcon = null

        }

        val view = toolbar as View

        // 设置距离状态高度
        if (addToolbarHeight) {
            val  a =view.layoutParams as MarginLayoutParams
            a.let {
                a.topMargin = getStatusBarHeight()
                view.layoutParams = a
            }
        }

    }

    fun setSubTitle(subTitle: String) {
        supportActionBar?.subtitle = subTitle
    }

    fun setToolbarButton(text: String, listener: View.OnClickListener) {

        with(findViewById<TextView>(R.id.tv_btn)) {
            this.visibility = View.VISIBLE
            this.text = text
            this.setOnClickListener(listener)
        }

    }


    open fun emptyViewOnListener() {
        Log.w(this.javaClass.name, "空视图点击事件未设置")
    }

    fun search(queryHint: String, onQueryTextListener: SearchView.OnQueryTextListener) {
        with(findViewById<SearchView>(R.id.search)) {
            this.visibility = View.VISIBLE
            this.queryHint = queryHint
            // 默认打开输入法
            this.setIconifiedByDefault(true)
            this.isFocusable = true
            this.isIconified = false
            this.requestFocusFromTouch()
            this.setOnQueryTextListener(onQueryTextListener)
        }
    }


}